#pragma once

#include <ctime>
#include <stdarg.h>
#include <stdio.h>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

enum
{
    Debug = 0,
    Info,
    Warning,
    Error,
    Fatal
};
std::string LevelToString(int level)
{
    switch (level)
    {
    case Debug:
        return "Debug";
    case Info:
        return "Info";
    case Warning:
        return "Warning";
    case Error:
        return "Error";
    case Fatal:
        return "Fatal";
    default:
        return "unknown";
    }
}
enum
{
    Screen = 10,
    Onefile,
    Classfile
};

const int defaultStyle = Screen;
const std::string defaultFileName = "log.";
const std::string logdir = "log";

class Log
{
public:
    Log() : _style(defaultStyle), _filename(defaultFileName)
    {
        mkdir(logdir.c_str(), 0775);
    }

    void Enable(int style) // 设置输出日志的模式
    {
        _style = style;
    }

    std::string ToLocalTime()
    {
        time_t currtime = time(nullptr);
        struct tm *curr = localtime(&currtime);
        char buffer[128];
        snprintf(buffer, sizeof(buffer), "%d-%d-%d %d:%d:%d",
                 curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday, curr->tm_hour, curr->tm_min, curr->tm_sec);
        return buffer;
    }

    void WriteLogToOnefile(const std::string &loginname, const std::string &loginStr)
    {
        umask(0);
        int fd = open(loginname.c_str(), O_CREAT | O_WRONLY | O_APPEND, 0666);
        if (fd < 0)
            return;
        write(fd, loginStr.c_str(), loginStr.size());
        close(fd);
    }

    void WriteLogToClassFile(const std::string &levelStr, const std::string &loginStr)
    {

        std::string loginname = logdir;
        loginname += "/";
        loginname += _filename;
        loginname += levelStr;
        WriteLogToOnefile(loginname, loginStr);
    }

    void WriteLog(const std::string &levelStr, const std::string &loginStr)
    {
        switch (_style)
        {
        case Screen:
            std::cout << loginStr;
            break;
        case Onefile:
            WriteLogToClassFile("all", loginStr);
            break;
        case Classfile:
            WriteLogToClassFile(levelStr, loginStr);
            break;
        default:
            break;
        }
    }
    void LogMessage(int level, const char *format, ...)
    {
        // leftBuffer = [日志等级][日志时间][日志pid]
        char leftBuffer[1024];
        std::string currtime = ToLocalTime();
        std::string pidStr = std::to_string(getpid());
        std::string levelStr = LevelToString(level);
        snprintf(leftBuffer, sizeof(leftBuffer), "[%s][%s][%s]", levelStr.c_str(),
                 currtime.c_str(), pidStr.c_str());

        // rightBuffer = 内容
        char rightBuffer[1024];
        va_list args;
        va_start(args, format);
        vsnprintf(rightBuffer, sizeof(rightBuffer), format, args);
        va_end(args);

        std::string loginfo;
        loginfo += leftBuffer;
        loginfo += rightBuffer;

        WriteLog(levelStr, loginfo);
    }

private:
    int _style; // 输出日志的模式
    std::string _filename;
};

Log lg;

class Conf
{
public:
    Conf()
    {
        lg.Enable(Screen);
    }
    ~Conf() {}
};

Conf conf;